/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.stylefeng.guns.core.common.constant.factory;

import cn.stylefeng.guns.core.common.constant.cache.Cache;
import cn.stylefeng.guns.core.common.constant.cache.CacheKey;
import cn.stylefeng.guns.core.common.constant.state.ManagerStatus;
import cn.stylefeng.guns.core.util.DgDateUtilLog;
import cn.stylefeng.guns.core.util.DgObjToMap;
import cn.stylefeng.guns.modular.system.dao.*;
import cn.stylefeng.guns.modular.system.model.*;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import java.util.Map;

/**
 * 常量的生产工厂
 *
 * @author fengshuonan
 * @date 2017年2月13日 下午10:55:21
 */
@Component
@DependsOn("springContextHolder")
public class DgConstantFactory implements IDgConstantFactory {
	private UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class);
	private DeptMapper deptMapper = SpringContextHolder.getBean(DeptMapper.class);
	private RoleMapper roleMapper = SpringContextHolder.getBean(RoleMapper.class);
	private ConstantFactory constantFactory = SpringContextHolder.getBean(ConstantFactory.class);
	public static IDgConstantFactory me() {
        return SpringContextHolder.getBean("dgConstantFactory");
    }
	
	/**
	  * <b>通过id查用户，并转成map
	  * </b><br><i>Description</i> :
	  * @param userId
	  * @return 
	  * <br>
	  * <br><b>Throws</b> :
	  * @date 2018年12月1日 下午7:06:08    <br><i>author</i> : dxl
	 */
	@Override
	public Map<String, String> getUserById(Integer  userId) {
		User user = userMapper.selectById(userId);
        if (user != null) {
            return DgObjToMap.objToMap(user);
        } else {
            return null;
        }
	}
	
	/**
	  * <b>通过sex标号查对应名称，用于包装，先判断sex是不是空，用于记录日志
	  * </b><br><i>Description</i> :
	  * @param sex
	  * @return String
	  * <br>
	  * <br><b>Throws</b> :
	  * @date 2018年12月1日 下午7:11:03    <br><i>author</i> : dxl
	 */
    @Override
    public String getSexName(String sex) {
    	if(sex != null && !"".equals(sex)){
    		Integer dgSex = Integer.valueOf(sex);
    		return constantFactory.getDictsByName("性别", dgSex);
    	}
		return "";
    }

    @Override
    public String dateStrToStr(String dateStr) {
    	if(dateStr != null && !"".equals(dateStr)){
    		return DgDateUtilLog.formatDate1(dateStr);
    	}
		return "";
    }
	/**
	 * <b>查询出人员状态对应的汉字（日志使用）
	 * </b><br><i>Description</i> :从数据库中查出来是1或2或3
	 * @param status
	 * @return String
	 * <br>
	 * <br><b>Throws</b> :
	 * @date 2018年12月1日 下午8:39:54    <br><i>author</i> : dxl
	 */
	@Override
	public String getStatusName(String status) {
		if(status != null && !"".equals(status)){
			Integer dgStatus = Integer.valueOf(status);
			return ManagerStatus.valueOf(dgStatus).toString();
		}
		return "";

	}
	/**
	 * <b>对应的部门名称（日志使用）
	 * </b><br><i>Description</i> :从数据库中查出来是1或2或3
	 * @param deptId
	 * @return String
	 * <br>
	 * <br><b>Throws</b> :
	 * @date 2018年12月1日 下午8:39:54    <br><i>author</i> : dxl
	 */
	@Override
	@Cacheable(value = Cache.CONSTANT, key = "'" + CacheKey.DEPT_NAME + "'+#deptId")
	public String getDeptName(String deptId) {
		if(deptId != null && !"".equals(deptId)){
			Integer dgDeptId = Integer.valueOf(deptId);
			Dept dept = deptMapper.selectById(dgDeptId);
			if (ToolUtil.isNotEmpty(dept) && ToolUtil.isNotEmpty(dept.getFullname())) {
				return dept.getFullname();
			}
		}
		return "";

	}
	/**
	 * <b>对应的角色名称（日志使用）
	 * </b><br><i>Description</i> :从数据库中查出来是数字ID
	 * @param roleId
	 * @return String
	 * <br>
	 * <br><b>Throws</b> :
	 * @date 2018年12月1日 下午8:39:54    <br><i>author</i> : dxl
	 */
	@Override
	public String getRoleName(String roleId){
		if(roleId != null && !"".equals(roleId)){
			Integer dgRoleId = Integer.valueOf(roleId);
			return roleMapper.selectById(dgRoleId).getName();
			}
		return "";
	}

}
